// MIT License (MIT)
// 
// Copyright (c) 2014 K Ercan Turkarslan
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// 

using System;
using System.Collections.Generic;
using System.Xml.Linq;
using CodeGen;

namespace MiosGenerator
{
    internal abstract class VariableCondition
    {
        internal abstract void Generate(CodeBlock cb, UpnpDataType argumentType, String argumentName);
    }

    internal class AllowedValueRange : VariableCondition
    {
        private string Minimum { get; set; }
        private string Maximum { get; set; }

        #region Generate Method
        internal override void Generate(CodeBlock cb, UpnpDataType argumentType, String argumentName)
        {
            if (!argumentType.IsNumeric) 
                return;

            var isMinimum = argumentType.IsMinValue(Minimum);
            var isMaximum = argumentType.IsMaxValue(Maximum);

            if (isMinimum && isMaximum)
                return;

            if (isMinimum)
            {
                cb.If(String.Format("{1} > {0}", Maximum, argumentName));
            }
            else if (isMaximum)
            {
                cb.If(String.Format("{1} < {0}", Minimum, argumentName));
            }
            else
            {
                cb.If(String.Format("({2} < {0}) || ({2} > {1})", Minimum, Maximum, argumentName));
            }
            cb.ThrowException("ArgumentOutOfRangeException", argumentName);
            cb.NewLine();
        }
        #endregion

        #region Constructors
        internal AllowedValueRange(XNode xNode)
        {
            Minimum = xNode.GetValue("minimum");
            Maximum = xNode.GetValue("maximum");
            // Step = xNode.GetValue("step");
        }
        #endregion
    }

    internal class AllowedValueList : VariableCondition
    {
        #region ReadOnly Property AllowedValues : Array of String;
        private readonly List<string> _allowedValues;

        private IEnumerable<string> AllowedValues
        {
            get
            {
                return _allowedValues.ToArray();
            }
        }
        #endregion

        #region Generate Method
        internal override void Generate(CodeBlock cb, UpnpDataType argumentType, String argumentName)
        {
            cb.Switch_Begin(String.Format("{0}{1}", argumentName, argumentType.IsString ? "" : ".ToString()"));

                foreach (var allowedValue in AllowedValues)
                {
                    cb.Switch_Case(allowedValue);
                }
                cb.Switch_Case_Break();
                cb.NewLine();

                cb.Switch_Default();
                cb.ThrowException("ArgumentOutOfRangeException", argumentName);

            cb.Switch_End();
            cb.NewLine();
        }
        #endregion

        #region Constructors
        internal AllowedValueList(XNode xNode)
        {
            _allowedValues = new List<string>();

            foreach (var childNode in xNode.GetChildren("allowedValueList"))
            {
                _allowedValues.Add(((XElement)childNode).Value);
            }
        }
        #endregion
    }
}